﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using MathUnity.Controllers;
using Microsoft.Practices.Unity;
using MvcContrib.Unity;
using MathUnity.Models;

namespace MathUnity
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication, IUnityContainerAccessor
    {
        private static UnityContainer _container;

        public static IUnityContainer Container
        {
            get { return _container; }
        }

        IUnityContainer IUnityContainerAccessor.Container
        {
            get { return Container; }
        }


        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            InitializeContainer();
            RegisterRoutes(RouteTable.Routes);
        }

        private void InitializeContainer()
        {
            if (_container==null)
            {
                ControllerBuilder.Current
                    .SetControllerFactory(typeof(UnityControllerFactory));
                _container = new UnityContainer();
                _container.RegisterType<IPiWebService, PiWebServiceWrapper>();
                _container.RegisterType<MathController>();
            }
        }
    }
}